﻿(function() {
    'use strict';

    var core = angular.module('skymate.core');
    core.controller('skymate.leftsidebar.ctrl', ['$http','$state', function ($http,$state) {

        var vm = this;

        function isActive(item) {

            if (!item.url || item.url === '#') {
                var foundActive = false;

                angular.forEach(item.items, function (value) {
                    if (isActive(value)) foundActive = true;
                });
                return foundActive;
            }
            else {
                return $state.is(item.url) || $state.includes(item.url);
            }

        }

        vm.getMenuItemPropClasses = function (item) {
            var css = (item.heading ? 'header' : '') +
                   (isActive(item) ? ' active ' : '');
            if (item.items.length > 0) {
                css = css + ' treeview ';
            }
            return css;
        };


        var menuUrl = "menu.json?t=" + new Date().getTime();
        function active() {

            $http.get(menuUrl).success(function(res) {
                vm.menuItems = res;
            });

        }



        active();

        return vm;

    }]);

})();